<?php
namespace wow\controller;

use \wow\model\dbcon as dbs;

use \wow\modelauth as auth;
use \wow\modelchar as chr;
use \gnomephp\message\Message;
use \gnomephp\doctrine\Doctrine;
class Application extends BaseController{


	
	

	public function index(){
		$bit = \wow\model\Page::getPage(\wow\model\Page::PAGE_MAIN_PAGE, $this->user, $this->view->getConfig('permission', 'main_page_edit'));
		$this->view->assign('dbpage', $bit);
		$this->view->render('index');
	}

	
	public function about(){

		$this->view->assign('dbpage', \wow\model\Page::getPage(\wow\model\Page::PAGE_ABOUT_PAGE, $this->user, $this->view->getConfig('permission', 'about_edit'),'About page, click to edit me', 3));
		$this->view->render('about');
	}
	
	
	public function realm($name, $id){
		$this->lang->load('mangos');
		// decompile url.
		$realm = auth\Realmlist::getRealmById($id);
		
		if (!$realm){
			Message::add(Message::ERROR, sprintf('The realm id:%d (name:%s) you requested is not valid.', $id, $name));
			return $this->index();
		}
		
		$this->view->assign('onlineChars',$realm->getCharacters('online = 1', 'level DESC'));
		$this->view->assign('bestPvPers',$realm->getCharacters('totalKills != 0', 'totalKills DESC', 10));
		
		
		
		
		$this->view->assign('realm',$realm);
		
		$this->view->render('realm/realm');	
	}
	
	public function saveText(){
		$id = $this->input->post->get('id');
		$text = $this->input->post->get('value');
		
		if ($page = \wow\model\Page::getPage($id, $this->user)){
			if ($page->canEditText()){
				$page->setText($text);
				Doctrine::load();
				Doctrine::getEM()->persist($page);
				Doctrine::getEM()->flush();
				echo $text;
			}
		}
		
		die();
	}
}